package com.example.q_kang.hhh.home;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.google.gson.Gson;
import com.hjq.toast.ToastUtils;
import com.sskj.base.MVPActivity;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.bean.BuyGamCommonBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.SPUtils;
import com.sskj.base.view.ToolBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/04/10
 */
public class BuyGamActivity extends MVPActivity<BuyGamPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolBarLayout;
    @BindView(R.id.cons0)
    ConstraintLayout cons0;
    @BindView(R.id.cons1)
    ConstraintLayout cons1;
    @BindView(R.id.cons2)
    ConstraintLayout cons2;
    @BindView(R.id.cons3)
    ConstraintLayout cons3;
    @BindView(R.id.cons4)
    ConstraintLayout cons4;
    @BindView(R.id.cons5)
    ConstraintLayout cons5;
    @BindView(R.id.cb_bank)
    CheckBox cb_bank;
    @BindView(R.id.cb_alipay)
    CheckBox cb_alipay;
    @BindView(R.id.cb_wechat)
    CheckBox cb_wechat;
    @BindView(R.id.iv_read_agreement)
    ImageView iv_read_agreement;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.ll_read_agreement)
    LinearLayout llReadAgreement;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_green_money)
    TextView tvGreenMoney;
    @BindView(R.id.tv_green_paidan)
    TextView tvGreenPaidan;
    @BindView(R.id.tv_blue_money)
    TextView tvBlueMoney;
    @BindView(R.id.tv_blue_paidan)
    TextView tvBluePaidan;
    @BindView(R.id.tv_red_money)
    TextView tvRedMoney;
    @BindView(R.id.tv_red_paidan)
    TextView tvRedPaidan;
    @BindView(R.id.tv_cat_money)
    TextView tvCatMoney;
    @BindView(R.id.tv_cat_paidan)
    TextView tvCatPaidan;
    private boolean isRead = true;

    private int buyType = 0;
    private List<String> payWays = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_buy_gam;
    }

    @Override
    public BuyGamPresenter getPresenter() {
        return new BuyGamPresenter();
    }

    @Override
    public void initView() {
//        toolBarLayout.setRightButtonOnClickListener(view -> );
        toolBarLayout.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, cons0, view -> {
            diamondChange(0);
            buyType = 0;
        });
        ClickUtil.click(300, cons1, view -> {
            diamondChange(1);
            buyType = 1;
        });
        ClickUtil.click(300, cons2, view -> {
            diamondChange(2);
            buyType = 2;
        });
        ClickUtil.click(300, cons3, view -> {
            diamondChange(3);
            buyType = 3;
        });
        ClickUtil.click(300, cons4, view -> {
            diamondChange(4);
            buyType = 4;
        });
        ClickUtil.click(300, cons5, view -> {
            diamondChange(5);
            buyType = 5;
        });
        ClickUtil.click(300, llReadAgreement, view -> {
            if (isRead) {
                isRead = false;
                iv_read_agreement.setImageResource(R.mipmap.icon_uncheck_small);
            } else {
                iv_read_agreement.setImageResource(R.mipmap.icon_check_small);
                isRead = true;
            }
        });
//        cb_bank.setOnCheckedChangeListener((compoundButton, b) -> {
//            if (!SPUtils.get(App.INSTANCE, SpConfig.BANK, "").equals("1")) {
//                ToastUtils.show(App.INSTANCE.getString(R.string.bindbank));
//                cb_bank.setChecked(false);
//            }
//        });
//        cb_alipay.setOnCheckedChangeListener((compoundButton, b) -> {
//            if (!SPUtils.get(App.INSTANCE, SpConfig.ALIPAY, "").equals("1")) {
//                ToastUtils.show(App.INSTANCE.getString(R.string.bindalipay));
//                cb_alipay.setChecked(false);
//            }
//        });
//        cb_wechat.setOnCheckedChangeListener((compoundButton, b) -> {
//            if (!SPUtils.get(App.INSTANCE, SpConfig.WECHAT, "").equals("1")) {
//                ToastUtils.show(App.INSTANCE.getString(R.string.bindwechat));
//                cb_wechat.setChecked(false);
//            }
//        });
        ClickUtil.click(300, btnSubmit, view -> {
            payWays.clear();
            if (!cb_bank.isChecked() && !cb_alipay.isChecked() && !cb_wechat.isChecked()) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaBuyGamActivity1));
                return;
            }
            if (cb_bank.isChecked()) {
                payWays.add("bank");
            }
            if (cb_alipay.isChecked()) {
                payWays.add("zfb");
            }
            if (cb_wechat.isChecked()) {
                payWays.add("wx");
            }
            Gson gson = new Gson();
            String s = gson.toJson(payWays);
            String s1 = s.replaceAll("\"", "");
            String substring = s1.substring(1, s1.length() - 1);
            if (TextUtils.isEmpty(getEtext(etPwd))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appXmlactivity_sell_gam286));
                return;
            }
            if (!isRead) {
                ToastUtils.show(App.INSTANCE.getString(R.string.readRule));
                return;
            }
            mPresenter.buyGam(buyType, substring, getEtext(etPwd));
        });
    }

    @Override
    public void initData() {
        mPresenter.getBuyGamCommonData();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BuyGamActivity.class);
        context.startActivity(intent);
    }

    private void diamondChange(int position) {
        cons0.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons1.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons2.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons3.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons4.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons5.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        switch (position) {
            case 0:
                cons0.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 1:
                cons1.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 2:
                cons2.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 3:
                cons3.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 4:
                cons4.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 5:
                cons5.setBackgroundResource(R.drawable.bg_list_blue);
                break;
        }
    }

    public void getDataSuccess(BuyGamCommonBean buyGamCommonBean) {
        if (buyGamCommonBean == null || buyGamCommonBean.getPrice() == null) {
            return;
        }
        if (buyGamCommonBean.getPrice().size() == 4) {
            tvGreenMoney.setText("$" + buyGamCommonBean.getPrice().get(0));
            tvBlueMoney.setText("$" + buyGamCommonBean.getPrice().get(1));
            tvRedMoney.setText("$" + buyGamCommonBean.getPrice().get(2));
            tvCatMoney.setText("$" + buyGamCommonBean.getPrice().get(3));
        }
        if (buyGamCommonBean.getNum().size() == 4) {
            tvGreenPaidan.setText(buyGamCommonBean.getNum().get(0) + App.INSTANCE.getString(R.string.appXmlfragment_home202));
            tvBluePaidan.setText(buyGamCommonBean.getNum().get(1) + App.INSTANCE.getString(R.string.appXmlfragment_home202));
            tvRedPaidan.setText(buyGamCommonBean.getNum().get(2) + App.INSTANCE.getString(R.string.appXmlfragment_home202));
            tvCatPaidan.setText(buyGamCommonBean.getNum().get(3) + App.INSTANCE.getString(R.string.appXmlfragment_home202));
        }
    }
}
